package com.xcc.opendd;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class SqlHelp extends SQLiteOpenHelper {
    public SqlHelp(Context context) {
        super(context, "OpenDD", null, 1);
    }

    public void onCreate(SQLiteDatabase db) {
        //初始化数据库的表结构 d integer primary key autoincrement,
        db.execSQL("create table timeTab(_id integer primary key autoincrement,timeL)");
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void insert(int timeL) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("insert into timeTab(timeL) values(?)", new Object[]{timeL});
        db.close();
    }

    public List<TimeMode> getList() {
        List<TimeMode> modeList = new ArrayList<>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from timeTab", null);
        while (cursor.moveToNext()) {
            TimeMode mode = new TimeMode();
            int idx = cursor.getColumnIndex("_id");
            if (idx == -1) continue;
            mode._id = cursor.getInt(idx);
            idx = cursor.getColumnIndex("timeL");
            if (idx == -1) continue;
            mode.timeL = cursor.getInt(idx);
            modeList.add(mode);
        }
        db.close();
        return modeList;
    }

    public void delete(int _id) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("delete from timeTab where _id=?", new Object[]{_id});
        db.close();
    }
}
